package com.flylee.util;

import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Iterator;

import javax.imageio.ImageIO;
import javax.imageio.ImageReadParam;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;

/**
 * java开发图像裁剪系统工具类
 * @author DELL
 *
 */
public class CutImagUtil {
	
	/**
	 * 图像裁剪工具类
	 * @param srcPath 原图片的路径
	 * @param targetPath 裁剪以后的图片路径
	 * @param x 坐标
	 * @param y 坐标
	 * @param w 宽度
	 * @param h 高度
	 * @throws IOException 
	 */
	public static void cutImage(String srcPath,String targetPath,int x,int y,int width,int height) throws IOException{
		//读取图片文件，建立文件输入流
		FileInputStream fis = null;
		ImageInputStream iis = null;
		try {
			fis = new FileInputStream(srcPath);
			
			//创建图片文件流的迭代器
			Iterator<ImageReader> it = ImageIO.getImageReadersByFormatName("jpg");
			ImageReader reader =it.next();
			//获取图片流，建立图片文件输入流
			iis = ImageIO.createImageInputStream(fis);
			reader.setInput(iis,true);
			ImageReadParam irp = reader.getDefaultReadParam();
			//定义裁剪区域
			Rectangle rect  = new Rectangle(x, y, width, height);
			irp.setSourceRegion(rect);
			
			BufferedImage bi = reader.read(0,irp);
			
			//保存图片
			ImageIO.write(bi, "jpg", new File(targetPath));
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("裁剪失败！");
		}finally {
			if(fis!=null){
				fis.close();
			}
			if(iis!=null){
				iis.close();
			}
			
		}
		
	}

}
